<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/7">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/59.shell 练习-22端口解封/">shell 练习-22端口解封</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>一个问题，不小心用iptables规则把sshd端口22给封掉了，结果不能远程登陆，要想解决这问题，还要去机房，登陆真机去删除这规则。 问题来了，要写个监控脚本，监控iptables规则是否封掉了22端口，如果封掉了，给打开。 写好脚本，放到任务计划里，每分钟执行一次。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment"># check sshd port drop</span></div><div class="line">/sbin/iptables -nvL --line-number|grep <span class="string">"dpt:22"</span>|awk -F <span class="string">' '</span> <span class="string">'&#123;print $4&#125;'</span> &gt; /tmp/drop.txt</div><div class="line">i=`cat /tmp/drop.txt|head -n 1|egrep -iE <span class="string">"DROP|REJECT"</span>|wc -l`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$i</span> -gt 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    /sbin/iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/58.shell 练习-文件打包/">shell 练习-文件打包</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>需求：将用户家目录（考虑到执行脚本的用户可能是普通用户也可能是root）下面小于5KB的文件打包成tar.gz的压缩包，并以当前日期为文件名前缀，例如今天打包的文件为2017-09-14.tar.gz。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">t=`date +%F`</div><div class="line"><span class="built_in">cd</span> <span class="variable">$HOME</span></div><div class="line">tar czf <span class="variable">$t</span>.tar.gz `find . -<span class="built_in">type</span> f -size -5k`</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/57.shell 练习-监控web可用性/">shell 练习-监控web可用性</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个shell脚本，通过curl -I 返回的状态码来判定所访问的网站是否正常。比如，当状态码为200时，才算正常。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#/bin/bash</span></div><div class="line">url=<span class="string">"http://www.apelearn.com/index.php"</span></div><div class="line">sta=`curl -I <span class="variable">$url</span> 2&gt;/dev/null |head -1 |awk <span class="string">'&#123;print $2&#125;'</span>`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$sta</span> != <span class="string">"200"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    python /usr/<span class="built_in">local</span>/sbin/mail.py xxx@qq.com <span class="string">"<span class="variable">$url</span> down."</span> <span class="string">"<span class="variable">$url</span> down"</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/56.shell 练习-监控网卡/">shell 练习-监控网卡</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<ol>
<li>每10分钟检测一次指定网卡的流量</li>
<li>如果流量为0，则重启网卡</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">LANG=en</div><div class="line">n1=`sar -n DEV 1 60 |grep eth0 |grep -i average|awk <span class="string">'&#123;print $5&#125;'</span>|sed <span class="string">'s/\.//g'</span>`</div><div class="line">n2=`sar -n DEV 1 60 |grep eth0 |grep -i average|awk <span class="string">'&#123;print $6&#125;'</span>|sed <span class="string">'s/\.//g'</span>`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n1</span> == <span class="string">"000"</span> ] &amp;&amp; [ <span class="variable">$n2</span> == <span class="string">"000"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    ifdown eth0</div><div class="line">    ifup eth0</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>然后写个cron，10分钟执行一次</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/55.shell 练习-判断日期合法/">shell 练习-判断日期合法</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>用shell脚本判断输入的日期是否合法。就是判断日期是都是真实的日期，比如20170110就是合法日期，20171332就不合法。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#check date</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -ne 1 ] || [ <span class="variable">$&#123;#1&#125;</span> -ne 8 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Usage: bash <span class="variable">$0</span> yyyymmdd"</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line">datem=<span class="variable">$1</span></div><div class="line">year=<span class="variable">$&#123;datem:0:4&#125;</span></div><div class="line">month=<span class="variable">$&#123;datem:4:2&#125;</span></div><div class="line">day=<span class="variable">$&#123;datem:6:2&#125;</span></div><div class="line"><span class="keyword">if</span> <span class="built_in">echo</span> <span class="variable">$day</span>|grep -q <span class="string">'^0'</span></div><div class="line"><span class="keyword">then</span></div><div class="line">    day=`<span class="built_in">echo</span> <span class="variable">$day</span> |sed <span class="string">'s/^0//'</span>`</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">if</span> cal <span class="variable">$month</span> <span class="variable">$year</span> &gt;/dev/null 2&gt;/dev/null</div><div class="line"><span class="keyword">then</span></div><div class="line">    daym=`cal <span class="variable">$month</span> <span class="variable">$year</span>|egrep -v <span class="string">"<span class="variable">$year</span>|Su"</span>|grep -w <span class="string">"<span class="variable">$day</span>"</span>`</div><div class="line">    <span class="keyword">if</span> [ <span class="string">"<span class="variable">$daym</span>"</span> != <span class="string">""</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> ok</div><div class="line">    <span class="keyword">else</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"Error: Please input a wright date."</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Error: Please input a wright date."</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/54.shell 练习-检查服务/">shell 练习-检查服务</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>先判断是否安装http和mysql，没有安装进行安装，安装了检查是否启动服务，若没有启动则需要启动服务。</p>
<p>说明：操作系统为centos6，httpd和mysql全部为rpm包安装。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">if_install()</div><div class="line">&#123;</div><div class="line">    n=`rpm -qa|grep -cw <span class="string">"<span class="variable">$1</span>"</span>`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 0 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> not install."</span></div><div class="line">    yum install -y <span class="variable">$1</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> installed."</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line">if_install httpd</div><div class="line">if_install mysql-server</div><div class="line">chk_ser()</div><div class="line">&#123;</div><div class="line">    p_n=`ps -C <span class="string">"<span class="variable">$1</span>"</span> --no-heading |wc -l`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$p_n</span> -eq 0 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> not start."</span></div><div class="line">    /etc/init.d/<span class="variable">$1</span> start</div><div class="line">    <span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> started."</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line">chk_httpd</div><div class="line">chk_mysqld</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/53.shell 练习-3位随机数字/">shell 练习-3位随机数字</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本产生随机3位的数字，并且可以根据用户的输入参数来判断输出几组。 比如，脚本名字为 number3.sh。<br>执行方法：<br>bash  number3.sh<br>直接产生一组3位数字。<br>bash number3.sh 10<br>插上10组3位数字。</p>
<p>思路： 可以使用echo $RANDOM获取一个随机数字，然后再除以10，取余获取0-9随机数字，三次运算获得一组。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">get_a_num</span></span>() &#123;</div><div class="line">    n=$[<span class="variable">$RANDOM</span>%10]</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$n</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">get_numbers</span></span>() &#123;</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> 1 2 3; <span class="keyword">do</span></div><div class="line">        a[<span class="variable">$i</span>]=`get_a_num`</div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$&#123;a[@]&#125;</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">if</span> [ -n <span class="string">"<span class="variable">$1</span>"</span> ]; <span class="keyword">then</span></div><div class="line">    m=`<span class="built_in">echo</span> <span class="variable">$1</span>|sed <span class="string">'s/[0-9]//g'</span>`</div><div class="line">    <span class="keyword">if</span> [ -n <span class="string">"<span class="variable">$m</span>"</span> ]; <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"Useage bash <span class="variable">$0</span> n, n is a number, example: bash <span class="variable">$0</span> 5"</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$1</span>`</div><div class="line">        <span class="keyword">do</span></div><div class="line">            get_numbers</div><div class="line">        <span class="keyword">done</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    get_numbers</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/52.shell 练习-shell的getops/">shell 练习-shell的getops</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个getinterface.sh 脚本可以接受选项[i，I]，完成下面任务：</p>
<ol>
<li>使用一下形式：getinterface.sh [-i interface | -I ip]</li>
<li>当用户使用-i选项时，显示指定网卡的IP地址；当用户使用-I选项时，显示其指定ip所属的网卡。</li>
</ol>
<p>例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sh getinterface.sh -i eth0</div><div class="line">       sh getinterface.sh -I 192.168.0.1</div></pre></td></tr></table></figure>
<ol>
<li>当用户使用除[-i | -I]选项时，显示[-i interface | -I ip]此信息。</li>
<li>当用户指定信息不符合时，显示错误。（比如指定的eth0没有，而是eth1时）</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">ip add |awk -F <span class="string">":"</span> <span class="string">'$1 ~ /^[1-9]/ &#123;print $2&#125;'</span>|sed <span class="string">'s/ //g'</span> &gt; /tmp/eths.txt</div><div class="line">[ -f /tmp/eth_ip.log ] &amp;&amp; rm -f /tmp/eth_ip.log</div><div class="line"><span class="keyword">for</span> eth <span class="keyword">in</span> `cat /tmp/eths.txt`</div><div class="line"><span class="keyword">do</span></div><div class="line">    ip=`ip add |grep -A2 <span class="string">": <span class="variable">$eth</span>"</span> |grep inet |awk <span class="string">'&#123;print $2&#125;'</span> |cut -d <span class="string">'/'</span> -f 1`</div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$eth</span>:<span class="variable">$ip</span>"</span> &gt;&gt; /tmp/eth_ip.log</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line">useage()</div><div class="line">&#123;</div><div class="line">    <span class="built_in">echo</span> <span class="string">"Please useage: <span class="variable">$0</span> -i 网卡名字 or <span class="variable">$0</span> -I ip地址"</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">wrong_eth()</div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span> ! grep -q <span class="string">"<span class="variable">$1</span>"</span> /tmp/eth_ip.log</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"请指定正确的网卡名字"</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">wrong_ip()</div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span> ! grep -qw <span class="string">"<span class="variable">$1</span>"</span> /tmp/eth_ip.log</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"请指定正确的ip地址"</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -ne 2 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    useage</div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">fi</span></div><div class="line"></div><div class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></div><div class="line">    -i)</div><div class="line">    wrong_eth <span class="variable">$2</span> </div><div class="line">    grep <span class="variable">$2</span> /tmp/eth_ip.log |awk -F <span class="string">':'</span> <span class="string">'&#123;print $2&#125;'</span></div><div class="line">    ;;</div><div class="line"></div><div class="line">    -I)</div><div class="line">    wrong_ip <span class="variable">$2</span></div><div class="line">    grep <span class="variable">$2</span> /tmp/eth_ip.log |awk -F <span class="string">':'</span> <span class="string">'&#123;print $1&#125;'</span></div><div class="line">    ;;</div><div class="line"></div><div class="line">    *)</div><div class="line">    useage</div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/51.shell 练习-三行变一行/">shell 练习-三行变一行</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>比如1.txt内容<br>1<br>2<br>3<br>4<br>5<br>6<br>7    </p>
<p>处理后应该是<br>1 2 3<br>4 5 6<br>7</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sed <span class="string">'N;N;s/\n/ /g'</span> 1.txt</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/5. shell 练习-还原 .bak备份文件/">shell 练习-还原 .bak备份文件</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>在你的/root 目录下有 .bak 后缀的备份文件，请写一脚本批量把这些文件都还原，也就是把.bak的后缀都去掉。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="built_in">cd</span> /root</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `ls|grep <span class="string">'bak'</span>`;<span class="keyword">do</span></div><div class="line">a=`<span class="built_in">echo</span> <span class="variable">$i</span>|awk -F<span class="string">'.bak'</span> <span class="string">'&#123;print $0&#125;'</span>`</div><div class="line">b=`<span class="built_in">echo</span> <span class="variable">$i</span>|awk -F<span class="string">'.bak'</span> <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line">mv <span class="variable">$a</span> <span class="variable">$b</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>





	<span class="different-posts">📖 <a href="/page/9">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
